<?php

/**
 * This is the model class for table "{{bus_station}}".
 *
 * The followings are the available columns in table '{{bus_station}}':
 * @property integer $bus_station_id
 * @property integer $bus_id
 * @property integer $direction
 * @property integer $ordering
 */
class BusStation extends CActiveRecord
{
    const DIRECTION_TRUE = 1;
    const DIRECTION_FALSE = 2;
    
    public $bus_search = NULL;
    public $station_search = NULL;

    /**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BusStation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{bus_station}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
//			array('bus_id, direction, ordering', 'required'),
			array('bus_id, station_id, direction, ordering', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('bus_search, station_search, bus_station_id, bus_id, direction, ordering', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'station' => array(self::BELONGS_TO, 'Station', 'station_id'),
                    'bus' => array(self::BELONGS_TO, 'Bus', 'bus_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'bus_station_id' => 'Bus Station',
			'bus_id' => 'Bus',
                        'station_id' => 'Station',
			'direction' => 'Direction',
			'ordering' => 'Ordering',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($iParentId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

                $criteria->with = array(
                    'bus',
                    'station'
                );
                $criteria->addCondition('t.bus_id = ' . (int) $iParentId);
                $criteria->order = 'ordering ASC';
                
                $criteria->compare('bus.name', $this->bus_search, TRUE);
                $criteria->compare('station.name', $this->station_search, TRUE);
		$criteria->compare('bus_station_id',$this->bus_station_id);
		$criteria->compare('bus_id',$this->bus_id);
		$criteria->compare('direction',$this->direction);
		$criteria->compare('ordering',$this->ordering);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}